package com.hotelsystem.reservations.hotels;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

import com.google.gson.Gson;
import com.hotelsystem.reservations.reservations.Reservation;
import com.hotelsystem.reservations.rooms.Room;

public class Hotel {

	private HotelInformation hotelInformation;
	private ArrayList<Room> rooms;
	private ArrayList<Reservation> reservations;

	public Hotel() {
		super();
		rooms = new ArrayList<Room>();
		reservations = new ArrayList<Reservation>();
	}

	public Hotel(HotelInformation hotelInformation, ArrayList<Room> rooms, ArrayList<Reservation> reservations) {
		super();
		this.hotelInformation = hotelInformation;
		this.rooms = rooms;
		this.reservations = reservations;
	}

	public HotelInformation getHotelInformation() {
		return hotelInformation;
	}

	public void setHotelInformation(HotelInformation hotelInformation) {
		this.hotelInformation = hotelInformation;
	}

	public ArrayList<Room> getRooms() {
		return rooms;
	}

	public void setRooms(ArrayList<Room> rooms) {
		this.rooms = rooms;
	}

	public ArrayList<Reservation> getReservations() {
		return reservations;
	}

	public void setReservations(ArrayList<Reservation> reservations) {
		this.reservations = reservations;
	}

	public void addReservation(Reservation reservation){
		reservations.add(reservation);
	}
	
	@Override
	public String toString() {
		return String.format(
				"Hotel [hotelInformation=%s, rooms=%s,\nreservations=%s]",
				hotelInformation, rooms, reservations);
	}
	/**
	 * Saves the container with the reservations from this hotel into a Json file 
	 * @param fileName - Name of the file.json
	 * @throws IOException
	 */
	public void SaveReservations(String fileName) throws IOException{
		Gson gson = new Gson();		
		BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
	    writer.write(gson.toJson(reservations));
	    writer.close();		 
	}
	/**
	 * Loads the container with the reservations from this hotel from the fileName Json file 
	 * @param fileName - Name of the file.json
	 * @throws IOException
	 */
	public void LoadReservations(String fileName) throws IOException{
		Gson gson = new Gson();				    
	  String jsn =new String(Files.readAllBytes(Paths.get(fileName)));
	  reservations = gson.fromJson(jsn, reservations.getClass());
	    		 
	}
	/**
	 * Saves the container with the roms from this hotel into a Json file 
	 * @param fileName - Name of the file.json
	 * @throws IOException
	 */
	public void SaveRooms(String fileName) throws IOException{
		Gson gson = new Gson();		
		BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
	    writer.write(gson.toJson(rooms));
	}
	/**
	 * Loads the container with the rooms from this hotel from the fileName Json file 
	 * @param fileName - Name of the file.json
	 * @throws IOException
	 */
	public void LoadRooms(String fileName) throws IOException{
		Gson gson = new Gson();				    
	  String jsn =new String(Files.readAllBytes(Paths.get(fileName)));
	  rooms = gson.fromJson(jsn, rooms.getClass());
	    		 
	}
	
}
